package com.hqyj.onlineExamination.modules.account.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.onlineExamination.modules.account.entity.User;
import com.hqyj.onlineExamination.modules.account.service.UserService;
import com.hqyj.onlineExamination.modules.common.entity.ResultEntity;
import com.hqyj.onlineExamination.modules.common.entity.SearchBean;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 127.0.0.1/api/login ---- post
     * {"userName":"admin", "password":"111111"}
     */
    @PostMapping(value = "/login", consumes = "application/json")
    public ResultEntity<User> login(@RequestBody User user) {
        return userService.login(user);
    }

    /**
     * 127.0.0.1/api/user ---- post
     * {"userName":"admin", "password":"111111"}
     */
    @PostMapping(value = "/user", consumes = "application/json")
    public ResultEntity<User> insertUser(@RequestBody User user) {
        return userService.insertUser(user);
    }

    /**
     * 127.0.0.1/api/user ---- put
     * {"id":"1", "userName":"admin", "password":"111111"}
     */
    @PutMapping(value = "/user", consumes = "application/json")
    public ResultEntity<User> updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }



    /**
     * 127.0.0.1:81/api/user/1 ---- get
     */

    @GetMapping("/user/{id}")
    public User getUserById(@PathVariable int id) {
        return userService.getUserById(id);
    }


    /**
     * 127.0.0.1/api/user/1 ---- delete
     */
    @DeleteMapping("/user/{id}")
    @RequiresPermissions(value={"deleteUser"})
    public ResultEntity<Object> deleteUserById(@PathVariable int id) {
        return userService.deleteUserById(id);
    }

    /**
     * 127.0.0.1:81/api/users ---- post
     * {"currentPage":"1","pageSize":"5","orderBy":"id","direction":"desc","keyWord":""}
     */
    @PostMapping(value = "/users", consumes = "application/json")
    public PageInfo<User> getUsersBySearchBean(@RequestBody SearchBean searchBean) {
        return userService.getUsersBySearchBean(searchBean);
    }

    /**
     * 127.0.0.1:81/api/users ---- get
     */
    @GetMapping("/users")
    public List<User> getUsers() {
        return userService.getUsers();
    }

    /**
     *查询是否重名
     */
    @GetMapping("/user")
    public User getUserByUserName(@RequestParam String userName) {
        return userService.getUserByUserName(userName);
    }
}
